package cn.wolfcode.luowowo.member.service.impl;

import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.common.exception.LogicException;
import cn.wolfcode.luowowo.common.util.AssertUtil;
import cn.wolfcode.luowowo.common.util.Consts;
import cn.wolfcode.luowowo.common.util.Md5;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.mapper.UserInfoMapper;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import java.util.List;
import java.util.UUID;

@Service
public class UserInfoServiceImpl implements IUserInfoService{

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Reference
    private IUserInfoRedisService userInfoRedisService;

    @Override
    public UserInfo get(Long id) {
        return userInfoMapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean checkPhone(String phone) {
        int count = userInfoMapper.selectCountByPhone(phone);
        return count>0;
    }

    //发送短信
    @Override
    public void sendVerifyCode(String phone) {
        //创建短信验证码
        String code = UUID.randomUUID().toString()
                .replaceAll("-", "")
                .substring(0, 4);
        //创建短信,发送短信
        StringBuilder sb = new StringBuilder(100);
        sb.append("您注册的短信验证码是").append(code).append(",请在").append(Consts.VERIFY_CODE_VAI_TIME).append("分钟内使用");
        //模拟发送短信
        System.out.println(sb);
        //将验证码缓存到redis中(redis相当于一个大的map,主要关心key和value的设计)
        userInfoRedisService.setVerifyCode(phone,code);

    }

    @Override
    public void userRegist(String phone, String nickname,
                           String password, String rpassword, String verifyCode) throws LogicException{

        //用户注册
        //判断参数是否为空
        AssertUtil.hasLength(phone,"手机号不能为空");
        AssertUtil.hasLength(nickname,"昵称号不能为空");
        AssertUtil.hasLength(password,"密码不能为空");
        AssertUtil.hasLength(rpassword,"重复密码不能为空");
        AssertUtil.hasLength(verifyCode,"验证码不能为空");

        //判断密码是否一致
        AssertUtil.isEquals(password,rpassword,"密码不一致");

        //判断手机号码格式是否正确
        boolean mobileNO = AssertUtil.isMobileNO(phone);
        if(!mobileNO){
            throw new LogicException("手机号格式错误");
        }

        //判断手机是否唯一
        if(this.checkPhone(phone)){
            throw new LogicException("手机号码以及被注册了");
        }
        //判断验证码是否一致

        String code  = userInfoRedisService.getVerifyCode(phone);
        if(code==null||!code.equalsIgnoreCase(verifyCode)){
            throw new LogicException("验证码错误");
        }
        //注册
        UserInfo userInfo = new UserInfo();
        userInfo.setHeadImgUrl("/images/default.jpg");
        userInfo.setLevel(1);
        userInfo.setNickname(nickname);
        userInfo.setPassword(Md5.md5(password));//密码加密
        userInfo.setPhone(phone);
        userInfo.setState(UserInfo.STATE_NORMAL);//状态值
        userInfoMapper.insert(userInfo);
    }

    @Override
    public String userLogin(String username, String password) throws LogicException {
        //查询账号密码是否存在
        AssertUtil.hasLength(username,"账号不能为空");
        AssertUtil.hasLength(password,"密码不能为空");
        UserInfo user = userInfoMapper.selectUserByUsernameAndPassword(username,Md5.md5(password));

        if(user==null){
            throw new LogicException("账号密码错误");
        }

        String token = UUID.randomUUID().toString().replaceAll("-","");

        userInfoRedisService.setToken(token,user);

        return token;
    }

    @Override
    public List<UserInfo> list() {
        return userInfoMapper.selectAll();
    }
}
